<?php
class ModelMenuMenu extends Model {
	public function addMenu($data) {
		if ($data['pid'] == 0) {
			$sublevel = 0;
		} else {
			$menu_info = $this->getMenuById($data['pid']);
			$sublevel = $menu_info['sublevel'] + 1;
		}
		$this->db->query("INSERT INTO menu SET pid = '" . $this->db->escape($data['pid']) 
		. "', menuname = '" . $this->db->escape($data['menuname']) 
		. "', ordering = '" . $this->db->escape($data['ordering']) 
		. "', sublevel = '" . $this->db->escape($sublevel) 
		. "', createdate = NOW()");
	}
	
	public function editMenu($menu_id, $data) {
		if ($data['pid'] == 0) {
			$sublevel = 0;
		} else {
			$menu_info = $this->getMenuById($data['pid']);
			$sublevel = $menu_info['sublevel'] + 1;
		}
		
		$this->db->query("UPDATE menu SET pid = '" . $this->db->escape($data['pid'])  
		. "', menuname = '" . $this->db->escape($data['menuname']) 
		. "', ordering = '" . $this->db->escape($data['ordering']) 
		. "', sublevel = '" . $this->db->escape($sublevel)
		. "', updatedate = NOW()"
		. " WHERE menu_id = '" . (int)$menu_id . "'");
	}
	
	public function deleteMenu($menu_id) {
		$this->db->query("DELETE FROM menu WHERE menu_id = '" . (int)$menu_id . "'");
	}
	
//	public function getTotalUsers($data = array()) {
//		$sql = "SELECT COUNT(*) AS total FROM user";
//
//		$query = $this->db->query($sql);
//
//		return $query->row['total'];
//	}

	public function getContentCountById($menu_id) {
		$sql = "SELECT count(*) AS total FROM content WHERE pid = '" . (int)$menu_id . "'";
	
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	
	public function getMenupCountById($menu_id) {
		$sql = "SELECT count(*) AS total FROM menu WHERE pid = '" . (int)$menu_id . "'";
	
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	
	public function getMenuById($menu_id) {
		$query = $this->db->query("SELECT * FROM menu WHERE menu_id = '" . (int)$menu_id . "'");
	
		return $query->row;
	}
	
	public function getTopMenus($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM menu WHERE pid = '0' ";
			
			$sort_data = array(
				'menuname',
				'ordering',
				'menu_id'
			);
				
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY menuname";
			}
				
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
				
			$query = $this->db->query($sql);

			return $query->rows;
		} else {
			$user_data = $this->cache->get('menu.' . $this->config->get('config_language_id'));

			if (!$user_data) {
				$query = $this->db->query("SELECT * FROM menu WHERE pid = '0' ORDER BY ordering ");

				$user_data = $query->rows;
					
				$this->cache->set('menu.' . $this->config->get('config_language_id'), $user_data);
			}

			return $user_data;
		}
	}
	
	//獲取指定分類下所有子分類ID
	function getIds($id,$strResult) {
		$sql = "SELECT * FROM menu WHERE pid = '$id'";
		$query = $this->db->query($sql);
		$results = $query->rows;
		foreach ($results as $result) {
			$strResult =  $strResult.$result['menu_id']."/";
			$strResult = $this->getIds($result['menu_id'],$strResult);
		}
		return $strResult;
	}
	//字符串轉數組
	function strToArray($str, $reg) {
		return explode($reg, $str);
	}
}
?>